import api from './api.js'
import CryptoJS from 'crypto-js'

const youdao = word => {
  const url = 'http://openapi.youdao.com/api'

  const appKey = '6fdd9de12d169e9a'
  const key = 't0HUyuso53615S9TTF7jLiVsD8ZrpklH'
  const salt = (new Date).getTime()
  const curtime = Math.round(new Date().getTime() / 1000)
  const query = word
  const from = 'en'
  const to = 'zh-CHS'
  const str1 = appKey + truncate(query) + salt + curtime + key
  const vocabId = 'frozen1bookstudy'
  const sign = CryptoJS.SHA256(str1).toString(CryptoJS.enc.Hex)
  const params = {
    q: query,
    appKey: appKey,
    salt: salt,
    from: from,
    to: to,
    sign: sign,
    signType: "v3",
    curtime: curtime,
    vocabId: vocabId,
  }
  api(url, params).then(res => {
    console.info('youdao res', res)
  })
}

function truncate (q) {
  var len = q.length
  if (len <= 20) return q
  return q.substring(0, 10) + len + q.substring(len - 10, len)
}

export default youdao